package com.sztx.pay.center.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import com.sztx.se.common.util.UUIDUtil;

public class MsgSrvUtil {
	
	public static String genConnId() {
		String head = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()).substring(13);
		String body = UUIDUtil.getUUID().substring(4);
		String connId = new StringBuilder().append(head + body).append(".").append(System.currentTimeMillis()).append(new Random().nextInt(10000))
				.toString();
		return connId;
	}
	
	public static String createMessage(String appName, String connId, String cmd, String data) {
		String msg = new StringBuilder().append(appName).append(" ").append(cmd).append(" ConnId=").append(connId).append(" ").append(data).toString();
		return msg;
	}

	public static String genMapSign(Map<String,Object> m, String exclude) {
		// add all parameters to tree
		TreeMap<String, Object> treeMap = new TreeMap<String, Object>();
		for(Map.Entry<String, Object> entry : m.entrySet()) {
			String key = entry.getKey();
			if(key.equals(exclude) || null == entry.getValue()) {
				continue;
			}else{
				treeMap.put(key, entry.getValue());
			}
		}
		StringBuffer sb = new StringBuffer();
		for (String key : treeMap.keySet()) {
			Object value = treeMap.get(key) == null ? "" : treeMap.get(key);
			sb.append(key).append("=").append(value).append("&");
		}
		String sign = sb.toString();
		return sign;
	}
	
}
